package class06;

import java.util.*;

/**
 * @description: 自定义比较器
 * @time: 2021/7/15 16:20
 * @author: lhy
 */
public class MyComparator {
    public static class Student {
        String name;
        int id;
        int age;
        public Student(String name, int id, int age){
            this.name = name;
            this.id = id;
            this.age = age;
        }
    }
    public static class IdComparator implements Comparator<Student> {
        /**
         * 按照学生的id从大到小排序
         * @param o1 第一个参数
         * @param o2 第二个参数
         * @return  如果返回正数，第二个参数排在前面
         *          如果返回负数，第一个参数排在前面
         *          如果返回0，谁在前无所谓
         */
        @Override
        public int compare(Student o1, Student o2) {
//            if (o1.id > o2.id){
//                return -1;
//            }else if (o1.id < o2.id){
//                return 1;
//            }else {
//                return 0;
//            }
            // return Integer.compare(o2.id, o1.id); 可以代替以上的if-else
            return o2.id - o1.id;
        }
    }
    public static class AgeComparator implements Comparator<Student> {
        /**
         * Integer.compare 默认按照升序排序
         * @param o1 第一个参数
         * @param o2 第二个参数
         * @return Integer.compare(o1.age,o2.age) 升序
         *         Integer.compare(o2.age,o1.age) 降序
         */
        @Override
        public int compare(Student o1, Student o2){
            //            return Integer.compare(o1.age,o2.age);
            return Integer.compare(o2.age,o1.age);
        }
    }
    public static void printStudent(Student[] students){
        for (Student s : students) {
            System.out.println("name:" + s.name + "\tid:" + s.id + "\tage:" + s.age);
        }
    }

    public static Student[] initStudent(){
        String[] names = {"Tom","Jack","Pink","Kang","Liu"};
        int[] ids = {202112101,202112102,202112104,202112105,202112103};
        int[] ages = {18,15,12,20,32};
        Student[] students = new Student[names.length];
        for (int i = 0; i < students.length; i++) {
            students[i] = new Student(names[i],ids[i],ages[i]);
        }
        return students;
    }
    public static void printArrayListStudent(ArrayList<Student> studentArrayList){
        for (Student s : studentArrayList) {
            System.out.println("name:" + s.name + "\tid:" + s.id + "\tage:" + s.age);
        }
    }

    /**
     * 默认是小根堆
     */
    public static void testPriorityQueue(){
        // test PriorityQueue
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        Integer[] arr = {1,3,5,2,6};
        Collections.addAll(heap,arr);
        // 返回最小的
        System.out.println(heap.peek());
        heap.add(0);
        System.out.println(heap.peek());
    }
    public static void main(String[] args) {
        Student[] students = initStudent();
        ArrayList<Student> studentArrayList = new ArrayList<>(Arrays.asList(students));
        PriorityQueue<Student> heap = new PriorityQueue<>(new IdComparator());
        Collections.addAll(heap, students);
        TreeSet<Student> treeSet = new TreeSet<>(new IdComparator());
        Collections.addAll(treeSet,students);

        // for test
        System.out.println(">>> Init students:");
        printStudent(students);
        System.out.println(">>> Sorted based on id:");
        Arrays.sort(students,new IdComparator());
        printStudent(students);

        System.out.println(">>> Sorted based on age by ArrayList:");
        studentArrayList.sort(new AgeComparator());
        printArrayListStudent(studentArrayList);

        System.out.println(">>> Sorted based on id by PriorityQueue:");
        while (!heap.isEmpty()){
            Student s = heap.poll();
            System.out.println("name:" + s.name + "\tid:" + s.id + "\tage:" + s.age);
        }

        System.out.println(">>> Sorted based on id by TreeSet:");
        for (Student s : treeSet) {
            System.out.println("name:" + s.name + "\tid:" + s.id + "\tage:" + s.age);
        }
        System.out.println(">>> String compareTo:");
        String s1 = "abc";
        String s2 = "b";
        /*
         * 字符串的比较按照 “字典序” 进行比较
         * 1) 2个字符串等长时，按照ASCII比较
         * 2) 2个字符串不等长时，将短的补齐（以最小的ASCII）再进行比较
         */
        System.out.println(s1.compareTo(s2));
    }
}
